﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CFGTool
{
    public partial class formMain : Form
    {
        /// <summary>
        /// 当前要设置的按键对象
        /// </summary>
        private Control CurrControl;

        public formMain()
        {
            InitializeComponent();
            InitKeyData();
            InitKeyColor();
            StatusSet(0);
        }
        private void formMain_Load(object sender, EventArgs e)
        {
            this.Text = "CFG生成工具V" + Application.ProductVersion.ToString();
        }
        /// <summary>
        /// 初始化每个按键的tag数据
        /// </summary>
        private void InitKeyData()
        {
            KeyData kd = null;
            foreach (Control item in panelKeyboard.Controls)
            {
                kd = new KeyData();
                string tmp = Properties.Resources.ResourceManager.GetString(item.Name);
                if (string.IsNullOrEmpty(tmp))
                {
                    kd.KeyName = "error";
                }
                else
                {
                    kd.KeyName = tmp;
                }
                kd.Value = new List<string>();
                item.Tag = kd;
            }
        }

        /// <summary>
        /// 初始化每个按键的颜色
        /// </summary>
        private void InitKeyColor()
        {
            foreach (Control item in panelKeyboard.Controls)
            {
                if (item.Tag == null || item.Tag.GetType() != typeof(KeyData))
                {
                    item.BackColor = Color.Red;
                }
                else
                {
                    item.BackColor = Control.DefaultBackColor;
                }
            }
        }

        /// <summary>
        /// 显示指定按钮的tag的数据
        /// </summary>
        /// <param name="control">按键</param>
        private void ShowTagData(Control control)
        {
            if (control.Tag != null)
            {
                if (control.Tag.GetType() == typeof(KeyData))
                {
                    tboxValue.Clear();
                    tboxValue.Text = string.Join("\r\n", ((KeyData)control.Tag).Value);
                }
            }
        }

        /// <summary>
        /// 设置控件状态
        /// 0:初始化状态
        /// 1:按钮设置状态
        /// </summary>
        /// <param name="code">状态代码</param>
        private void StatusSet(int code)
        {
            //初始状态
            if (code == 0)
            {
                buttCancel.Enabled = false;
                buttSave.Enabled = false;
                panelKeyboard.Enabled = true;
                tcMain.Enabled = false;
                tboxValue.Clear();
                CurrControl = new Control();
            }

            //设置按键时状态
            if (code == 1)
            {
                buttCancel.Enabled = true;
                buttSave.Enabled = true;
                panelKeyboard.Enabled = false;
                tcMain.Enabled = true;
                
            }
            
        }

        /// <summary>
        /// 取消选择框的选择状态
        /// </summary>
        private void UnCheck()
        {
            foreach (Control item in tpOption.Controls)
            {
                if (item.GetType() == typeof(CheckBox))
                {
                    ((CheckBox)item).Checked = false;
                }
            }
            foreach (Control item in tpBuys.Controls)
            {
                if (item.GetType() == typeof(CheckBox))
                {
                    ((CheckBox)item).Checked = false;
                }
            }
        }

        /// <summary>
        /// 虚拟键盘按钮处理
        /// </summary>
        private void butt_Click(object sender, EventArgs e)
        {
            Button buttTmp = sender as Button;
            buttTmp.BackColor = Color.BlueViolet;
            CurrControl = buttTmp;
            StatusSet(1);
            ShowTagData(buttTmp);
            /*
            foreach (Control item in this.panelKeyboard.Controls)
            {
                if (item.Focused)
                {
                    item.BackColor = Color.BlueViolet;
                    CurrControl = item;
                    StatusSet(1);
                    ShowTagData(item);
                }
            }
            */
        }

        /// <summary>
        /// 保存按键设置
        /// </summary>
        private void buttSave_Click(object sender, EventArgs e)
        {
            //保存数据
            ((KeyData)CurrControl.Tag).Value.Clear();
            foreach (var item in tboxValue.Lines)
            {
                ((KeyData)CurrControl.Tag).Value.Add(item.ToString());
            }
           
            //显示有绑定数据的键的颜色
            foreach (Control item in this.panelKeyboard.Controls)
            {
                if (item.Tag == null || item.Tag.GetType() != typeof(KeyData))
                {
                    item.BackColor = Color.Red;
                }
                else
                {
                    if (((KeyData)item.Tag).HaveData)
                    {
                        item.BackColor = Color.Green;
                    }
                    else
                    {
                        item.BackColor = Control.DefaultBackColor;
                    }
                }
            }
            UnCheck();
            StatusSet(0);
        }

        /// <summary>
        /// 取消按键设置
        /// </summary>
        private void buttCancel_Click(object sender, EventArgs e)
        {
            foreach (Control item in this.panelKeyboard.Controls)
            {
                if (item.Tag == null || item.Tag.GetType() != typeof(KeyData))
                {
                    item.BackColor = Color.Red;
                }
                else
                {
                    if (((KeyData)item.Tag).HaveData)
                    {
                        item.BackColor = Color.Green;
                    }
                    else
                    {
                        item.BackColor = Control.DefaultBackColor;
                    }
                }
            }
            UnCheck();
            StatusSet(0);
        }


        /// <summary>
        /// 选项按键
        /// </summary>
        private void tbS_Click(object sender, EventArgs e)
        {
            if (!tcMain.Enabled) return;
            if (CurrControl == null) return;
            string tmpData = "";
            KeyData tmpKD = CurrControl.Tag as KeyData;
            if (sender.GetType() == typeof(Button))
            {
                tmpData = Properties.Resources.ResourceManager.GetString(((Button)sender).Tag.ToString());
                tboxValue.Text = tmpData.Replace("xxxKey", tmpKD.KeyName);
            }
        }

        /// <summary>
        /// 选项选择按键
        /// </summary>
        private void tcO_CheckedChanged(object sender, EventArgs e)
        {
            tboxValue.Clear();
            KeyData tmpKD = CurrControl.Tag as KeyData;
            string tmpData = "bind \"" + tmpKD.KeyName + "\" \"";
            foreach (Control item in tpOption.Controls)
            {
                if (item.GetType() != typeof(CheckBox)) continue;
                if (((CheckBox)item).Checked)
                {
                    tmpData = tmpData + item.Tag.ToString();
                }
            }
            tmpData = tmpData + "\";";
            tboxValue.Text = tmpData;
        }
        /// <summary>
        /// 购买物品按键
        /// </summary>
        private void tcB_CheckedChanged(object sender, EventArgs e)
        {
            tboxValue.Clear();
            KeyData tmpKD = CurrControl.Tag as KeyData;
            string tmpData = "bind \"" + tmpKD.KeyName + "\" \"";
            foreach (Control item in tpBuys.Controls)
            {
                if (item.GetType() != typeof(CheckBox)) continue;
                if (((CheckBox)item).Checked)
                {
                    tmpData = tmpData + item.Tag.ToString();
                }
            }
            tmpData = tmpData + "\";";
            tboxValue.Text = tmpData;
        }

        /// <summary>
        /// 生成CFG
        /// </summary>
        private void 生成CFGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Control item in panelKeyboard.Controls)
            {
                KeyData tmpKD = ((Button)item).Tag as KeyData;
                if (!tmpKD.HaveData) continue;
                sb.AppendLine(string.Join("\r\n", tmpKD.Value)).AppendLine();
            }
            //MessageBox.Show(sb.ToString());
            new formShow(sb.ToString()).ShowDialog();
        }

        private void 帮助说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://git.oschina.net/alwcel/CFGTool");
        }
    }
}
